Repository navigation
Conversation
This change validates the detected directory in VmServiceHandler before caching it or updating DTD, requiring that it: is a local file:// URI with no authority, query, fragment, or dot-segment traversal (. / ..), does not point or resolve (via symlinks) to the filesystem root or the user's home directory (HOME / USERPROFILE), and exists on disk and contains either pubspec.yaml or .dart_tool/package_config.json. It also extracts shared isValidLocalFileUri and isValidLocalFileUriString helpers in file_utils.dart (reused by ExtensionsManager and the extensions API handler) and adds unit and server API tests covering valid roots, invalid URI forms, filesystem/home root rejection, missing package markers, and end-to-end /api/notifyForVmServiceConnection handling. Makes progress towards: * flutter/dash-devexp-internal#88 * flutter/dash-devexp-internal#93
There was a problem hiding this comment.
Code Review
This pull request introduces package root validation in VmServiceHandler before caching or updating Dart Tooling Daemon (DTD) workspace roots, utilizing new local file URI validation helpers to prevent path traversal and reject invalid roots (such as the filesystem root or user home directory). The review feedback highlights a critical logical issue where validating the package root during disconnect flows can prevent proper cleanup of DTD workspace roots if the directory or its package markers were deleted during the session. It is recommended to skip validation when connected is false to ensure stale roots are successfully removed.
… validation. * Reject encoded slashes (`/` / `%2f`) in `isValidLocalFileUri` path segments and validate raw `root` strings with `isValidLocalFileUriString` before `Uri.parse` in `detectRootPackageForVmService`, with new `%2f` test coverage. * Simplify `_validateRootPackageUri` to check `dir.existsSync()` first, resolve symlinks once (failing closed on `FileSystemException`), and compare only the resolved path against the filesystem root and resolved home directory. * Remove duplicate `_validateRootPackageUri` validation from `updateDtdWorkspaceRoots` and move its rejection tests to standalone `isValidRootPackageUri` unit tests without DTD. * Extract the repeated `runWithRetry` polling blocks in `detectRootPackageForVmService` tests into a shared `detectRootWithRetry` helper. * Update invalid-URI error messages in `_vm_service.dart` and `_devtools_extensions.dart`, add doc comments to `VmServiceHandler` constants, remove redundant `@visibleForTesting`, and document the new helpers and stricter URI checks in `packages/devtools_shared/CHANGELOG.md`.
- Move `_invalidLocalFileUriMessage` to a library-private constant in `server_api.dart` and reuse it in `_devtools_extensions.dart` and `_vm_service.dart` so invalid local file URI error messages stay in sync. - Expose `VmServiceHandler.validateRootPackageUri` as `@visibleForTesting`, remove `isValidRootPackageUri`, and assert on specific rejection reason messages in `general_api_test.dart`. - Trim the invalid local file URI test in `general_api_test.dart` to a single query URI case since `file_utils_test.dart` already covers all invalid URI components. - Restore plain `Uri.parse` fixtures in `updateDtdWorkspaceRoots` tests and remove the unnecessary temporary directory setup and deleted-directory test. - Document `detectRootWithRetry`'s default `isReady` behavior and simplify its `isReady` callbacks to use null-aware `contains`.
…e-roots # Conflicts: # packages/devtools_shared/CHANGELOG.md # packages/devtools_shared/lib/src/server/handlers/_devtools_extensions.dart # packages/devtools_shared/lib/src/server/handlers/_vm_service.dart # packages/devtools_shared/test/server/general_api_test.dart
|
@kenzieschmoll @bkonyi this is ready for another look |
| bool isValidLocalFileUri(Uri uri) { | ||
| if (uri.scheme != 'file' || | ||
| uri.authority.isNotEmpty || | ||
| !uri.hasAbsolutePath || |
There was a problem hiding this comment.
This still lets Windows UNC paths slip through via four leading slashes (e.g. file:////server/share/my_app). That parses with an empty authority and path == '//server/share/my_app', which uri.toFilePath() turns into \\server\share\my_app on Windows, reaching Directory.existsSync() (and even registering file://server/share/my_app with DTD if pubspec.yaml exists on the share).
Can we also reject uri.path.startsWith('//') here and add a test case for file:////server/share/my_app in file_utils_test.dart?
| ); | ||
| } | ||
|
|
||
| return (resolvedUri: Uri.file(resolvedDirPath), error: null); |
There was a problem hiding this comment.
On Windows, resolveSymbolicLinksSync() can resolve a symlink to a UNC share (or a mapped network drive) into \\server\share\..., so Uri.file(resolvedDirPath) ends up as file://server/share/.... That violates isValidLocalFileUri and will cause ExtensionsManager._assertUriFormat to throw an ArgumentError later. Can we construct resolvedUri = Uri.file(resolvedDirPath) right after resolving symlinks and check isValidLocalFileUri(resolvedUri) before proceeding?
| } | ||
|
|
||
| /// Error description for invalid local `file://` URIs. | ||
| const invalidLocalFileUriMessage = |
There was a problem hiding this comment.
Nit: do we want invalidLocalFileUriMessage (a lowercase error-message fragment) exported as public API from devtools_shared.dart? Since all callers live inside devtools_shared, I'd lean toward hide-ing it in devtools_shared.dart (and dropping it from the CHANGELOG.md entry).
|
|
||
| /// Resolves symbolic links for [directory] if it exists, returning `null` if | ||
| /// it does not exist or resolution fails. | ||
| static String? _tryResolveSymbolicLinks(Directory directory) { |
There was a problem hiding this comment.
Nit: resolveSymbolicLinksSync() already throws FileSystemException when the directory doesn't exist, so existsSync() here is redundant. Since this only has one caller, I'd probably inline the try/on FileSystemException in _isUserHomeOrAncestorDirectory.
| uri: null, | ||
| ); | ||
| } | ||
| final validationResult = validateRootPackageUri(parsedRoot); |
There was a problem hiding this comment.
Nit: we can destructure the record and check resolvedUri == null to avoid the !:
final (:resolvedUri, :error) = validateRootPackageUri(parsedRoot);
if (resolvedUri == null) {
return (success: false, message: error, uri: null);
}
rootPackageUri = resolvedUri;
This change validates and resolves symlinks for the detected directory in
VmServiceHandlerbefore caching the resolved URI or updating DTD, requiring that it:file://URI with no authority, query, fragment, or null bytes that converts cleanly to a local file pathHOME/USERPROFILE), or an ancestor of it, and exists on diskpubspec.yamlor.dart_tool/package_config.json.It also extracts shared
isValidLocalFileUri,isValidLocalFileUriString, andinvalidLocalFileUriMessagehelpers infile_utils.dart(reused byExtensionsManagerand the extensions API handler) and adds unit and server API tests covering valid and symlink-resolved roots, invalid URI forms, filesystem/home/home-ancestor root rejection, missing package markers, and end-to-end/api/notifyForVmServiceConnectionhandling.Makes progress towards: